<?php ob_start();
	require_once('../../../Connections/microassur.php'); 
	require_once('../../utils/fonctions.php');	
	$message = '';
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if( isset($_POST['Valeur_coordonnee_communication']) && empty($_POST['Valeur_coordonnee_communication']) ){
	$message = colorer("Vous devez saisir la VALEUR de cette adresse.","false");
}else{
	if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	  $updateSQL = sprintf("UPDATE coordonneescommunication SET Type_coordonnees_communication=%s, Etat_coordonnees_communication=%s, Valeur_coordonnee_communication=%s, Identifiant_personne=%s WHERE Identifiant_communication=%s",
						   GetSQLValueString($_POST['Type_coordonnees_communication'], "text"),
						   GetSQLValueString($_POST['Etat_coordonnees_communication'], "text"),
						   GetSQLValueString($_POST['Valeur_coordonnee_communication'], "text"),
						   GetSQLValueString($_POST['Identifiant_personne'], "int"),
						   GetSQLValueString($_POST['Identifiant_communication'], "int"));
	
	  mysql_select_db($database_microassur, $microassur);
	  mysql_query("START TRANSACTION");
	  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());
	  if($Result1){
	  	mysql_query("commit");
		$message = colorer("La modification de cette communication est prise en compte.","true");
	  }else{
	  	mysql_query("rollback");
		$message = colorer("Problème modification de la communication, contacter la DSI.","false");
	  }
	}
}

// Suppression de la communication (rendre l'état de la communication à Inactif : I)
if(isset($_GET["supp"]) && $_GET["supp"]=="1" && isset($_GET["Identifiant_communication"]) && isset($_GET["Identifiant_communication"])){
	//var_dump($_GET);
	$_POST['Etat_coordonnees_communication'] = 'I';
	$updateSQL2 = "UPDATE coordonneescommunication SET Etat_coordonnees_communication='I' WHERE Identifiant_communication=" . GetSQLValueString($_GET['Identifiant_communication'], "int") . " ;";
	mysql_select_db($database_microassur, $microassur);
	mysql_query("START TRANSACTION");
	$Result2 = mysql_query($updateSQL2, $microassur) or die(mysql_error());	
	if($Result2){
		mysql_query("commit");
		//$message = colorer("Communication supprimée.","true");
			$deleteGoTo = "index.php?Identifiant_communication=". $_GET['Identifiant_communication'] ."&Identifiant_personne=". $_GET['Identifiant_personne'] ."&msg=supp";
			header(sprintf("Location: %s", $deleteGoTo));
		unset($_POST);
	}else{
		mysql_query("rollback");
		$message = colorer("Problème suppression communication, contacter la DSI .","false");
	}
}

$colname_rs_mod = "-1";
if (isset($_GET['Identifiant_communication'])) {
  $colname_rs_mod = $_GET['Identifiant_communication'];
}
mysql_select_db($database_microassur, $microassur);
$query_rs_mod = sprintf("SELECT * FROM coordonneescommunication WHERE Identifiant_communication = %s", GetSQLValueString($colname_rs_mod, "int"));
$rs_mod = mysql_query($query_rs_mod, $microassur) or die(mysql_error());
$row_rs_mod = mysql_fetch_assoc($rs_mod);
$totalRows_rs_mod = mysql_num_rows($rs_mod);

mysql_select_db($database_microassur, $microassur);
$query_rs_personne = "SELECT * FROM personne";
$rs_personne = mysql_query($query_rs_personne, $microassur) or die(mysql_error());
$row_rs_personne = mysql_fetch_assoc($rs_personne);
$totalRows_rs_personne = mysql_num_rows($rs_personne);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title>Modification coordonneés de communication</title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
<div id="sf_menu1">
	<h3>Intervenant <?php //echo nomPersonne($database_microassur, $microassur, $_GET['Identifiant_personne']) ?></h3>
<?php
	if(isset($message) && !empty($message)){
		echo $message;
	}
	$nommodule="Communication";
	//$nommodule2="Liste des partenaires";
	$nom_module_saisie="Détail communication";
?>
</div>
<div class="cleared"></div>

<div class="corps1">
<!--/***************************************/-->

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
	<div class="imgleft"></div>
	<div class="imgmiddle">
    <div class="btn">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nommodule; ?></h5>
        </span><span class="btnright"></span>
    </div>
  	<div class="btn1">
    	<span class="btnleft"></span>
        <span class="btnmiddle">
        	<h5><?php echo $nom_module_saisie; ?></h5>
        </span>
        <span class="btnright"></span>
    </div>
	<div class="formulaire">
  <table class="tabform">
    <tr>
      <td class="td0">TYPE COORDONNEE</td>
      <td><select name="Type_coordonnees_communication">
        <option value="EM" <?php if (!(strcmp("EM", htmlentities($row_rs_mod['Type_coordonnees_communication'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>E-mail</option>
        <option value="FA" <?php if (!(strcmp("FA", htmlentities($row_rs_mod['Type_coordonnees_communication'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Fax</option>
        <option value="TB" <?php if (!(strcmp("TB", htmlentities($row_rs_mod['Type_coordonnees_communication'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Téléphone bureau</option>
        <option value="TD" <?php if (!(strcmp("TD", htmlentities($row_rs_mod['Type_coordonnees_communication'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Téléphone domicile</option>
        <option value="TP" <?php if (!(strcmp("TP", htmlentities($row_rs_mod['Type_coordonnees_communication'], ENT_COMPAT, 'utf-8')))) {echo "SELECTED";} ?>>Téléphone portable</option>
      </select>
      </td>
    </tr>
	<input type="hidden" name="Etat_coordonnees_communication" value="<?php echo htmlentities($row_rs_mod['Etat_coordonnees_communication'], ENT_COMPAT, 'utf-8'); ?>" />
    <tr>
      <td class="td0">VALEUR</td>
      <td><input type="text" name="Valeur_coordonnee_communication" value="<?php echo htmlentities($row_rs_mod['Valeur_coordonnee_communication'], ENT_COMPAT, 'utf-8'); ?>" size="32" maxlength="30" /></td>
    </tr>
    <input type="hidden" name="Identifiant_personne" value="<?php echo htmlentities($row_rs_mod['Identifiant_personne'], ENT_COMPAT, 'utf-8'); ?>" />
  </table>
    </div>
</div>
<div class="imgright"></div>				
<div class="cleared"></div>

<div class="bt1">
	<a href="../communication/index.php?Identifiant_personne=<?php echo $_GET['Identifiant_personne']; ?>">
    	<span class="btleft"></span>
        <span class="btmiddle">Annuler</span>
        <span class="btright"></span>
    </a>
</div>
<!--div class="bt">
	<a href="modcommunication.php?Identifiant_communication=<?php echo $row_rs_mod['Identifiant_communication']; ?>&Identifiant_personne=<?php echo $row_rs_mod['Identifiant_personne']; ?>&supp=1">
	<span class="btleft"></span>
    <span class="btmiddle">Supprimer</span>
    <span class="btright"></span>
    </a>
</div-->
<div class="bt">
	<span class="btleft"></span>
    <input type="submit" name="Valider" value="Valider" class="resizeall btmiddle"/>
    <span class="btright"></span>
</div>
    
<div class="cleared"></div>

  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="Identifiant_communication" value="<?php echo $row_rs_mod['Identifiant_communication']; ?>" />
</form>
 </div>
</body>
</html>
<?php
mysql_free_result($rs_mod);

mysql_free_result($rs_personne);

ob_end_flush();
?>